package io.geekidea.springbootplus.system.controller;


import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.Advertise;
import io.geekidea.springbootplus.system.param.AdvertisePageParam;
import io.geekidea.springbootplus.system.param.AdvertiseStatusParam;
import io.geekidea.springbootplus.system.param.sysuser.SysUserPageParam;
import io.geekidea.springbootplus.system.service.AdvertiseService;
import io.geekidea.springbootplus.system.vo.AdvertiseQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1")
@Module("system")
@Api(value ="首页轮播管理API" ,tags = "轮播管理")
public class AdvertiseController extends BaseController {

    @Resource
    private AdvertiseService advertiseService;

    @PostMapping("/advertise/add")
//    @RequiresPermissions("sys:advertise:add")
    @OperationLog(value = "新增轮播图" ,type = OperationLogType.ADD)
    @ApiOperation(value = "新增轮播图",response = ApiResult.class)
    public ApiResult<Boolean> insertSysAdvertise(@RequestBody AdvertiseQueryVo sysAdvertise) throws Exception {

        boolean result = advertiseService.addSysAdvertise(sysAdvertise);
        return ApiResult.result(result);
    }

    @PostMapping("/advertise/update")
//    @RequiresPermissions("sys:advertise:update")
    @OperationLog(value = "修改轮播图" ,type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改轮播图",response = ApiResult.class)
    public ApiResult<Boolean> updateSysAdvertise(@RequestBody AdvertiseQueryVo advertiseQueryVo) throws Exception {
        boolean result = advertiseService.updateSysAdvertise(advertiseQueryVo);
        return ApiResult.result(result);
    }

    @PostMapping("/advertise/updateStatus")
//    @RequiresPermissions("sys:advertise:update")
    @OperationLog(value = "上架下架" ,type = OperationLogType.UPDATE)
    @ApiOperation(value = "上架下架",response = ApiResult.class)
    public ApiResult<Boolean> updateSysAdvertiseStatus(  @RequestBody AdvertiseStatusParam advertiseStatusParam ) throws Exception {

        boolean result = advertiseService.updateAdvertiseStatus(advertiseStatusParam);
        return ApiResult.result(result);
    }

    @PostMapping("/advertise/delete/{id}")
//    @RequiresPermissions("sys:advertise:delete")
    @OperationLog(value = "删除轮播图" ,type = OperationLogType.ADD)
    @ApiOperation(value = "删除轮播图",response = ApiResult.class)
    public ApiResult<Boolean> deleteSysAdvertiseById(@PathVariable("id") int  id) throws Exception {
        boolean result = advertiseService.deleteSysAdvertiseById(id);
        return ApiResult.result(result);
    }

    @PostMapping("/advertise/delete/batch")
//    @RequiresPermissions("sys:advertise:batch")
    @OperationLog(value = "批量删除轮播图" ,type = OperationLogType.DELETE_BATCH)
    @ApiOperation(value = "清空轮播图",response = ApiResult.class)
    public ApiResult<Boolean> deleteAllSysAdvertise(@RequestBody List<Integer> ids) throws Exception {
        boolean result = advertiseService.deleteAllAdvertise(ids);
        return ApiResult.result(result);
    }

    @PostMapping("/advertise/list")
//    @RequiresPermissions("sys:advertise:list")
    @OperationLog(name = "轮播图查询",type = OperationLogType.LIST)
    @ApiOperation(value = "分页查询所有轮播图",response = AdvertiseQueryVo.class)
    public ApiResult getSysAdvertisePageList(@Validated @RequestBody AdvertisePageParam advertisePageParam  ) throws Exception {
        Paging<AdvertiseQueryVo> page = advertiseService.getSysAdvertisePageList(advertisePageParam);
        return  ApiResult.ok(page);
    }

    @ApiOperation(value = "查询单个轮播图",response = AdvertiseQueryVo.class)
    @GetMapping("/advertise/selectOne/{id}")
    public ApiResult getAdvertiseById( @PathVariable  Integer id){
        AdvertiseQueryVo advertise = advertiseService.findAdvertiseById(id);   //获取单个对象 可以用来做判断条件
        return ApiResult.ok(advertise);
    }
}
